package com.tourlaid.mamagerment.ao.pc.impl

import com.github.pagehelper.Page
import com.github.pagehelper.PageHelper
import com.tourlaid.mamagerment.ao.pc.SupplierAo
import com.tourlaid.mamagerment.entity.ResponseData
import com.tourlaid.mamagerment.entity.Supplier
import com.tourlaid.mamagerment.service.SupplierService
import com.tourlaid.mamagerment.util.DateUtil
import com.tourlaid.mamagerment.vo.SupplierListVo
import com.tourlaid.mamagerment.vo.SupplierVo
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import org.springframework.util.CollectionUtils
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/7/23.
 */
@Service("supplierAo")
class SupplierAoImpl : SupplierAo {

    @Resource
    var supplierService: SupplierService? = null

    override fun list(pageNo: Int, pageSize: Int): ResponseData<ArrayList<SupplierListVo>> {
        var voList = ArrayList<SupplierListVo>()
        var responseData = ResponseData(voList)
        PageHelper.startPage<Supplier>(pageNo, pageSize, true)
        var page = supplierService!!.queryForList()!! as Page<Supplier>
        responseData.totalCount = page.getTotal()
        val supplierList = page.getResult()
        if (!CollectionUtils.isEmpty(supplierList)) {
            for (supplier in supplierList) {
                var vo = SupplierListVo()
                BeanUtils.copyProperties(supplier, vo)
                vo.createTime = DateUtil.getFormantDate(supplier.createTime, DateUtil.DATA_FORMANT3)
                voList.add(vo)
            }
        }
        return responseData
    }

    override fun insert(supplierName: String?, controlNo: String?) {
        supplierService!!.insert(supplierName, controlNo)
    }

    override fun detail(supplierId: Long?): SupplierVo {
        var vo = SupplierVo()
        var supplier = supplierService!!.getById(supplierId)
        supplier ?: return vo
        vo.id = supplier.id
        vo.supplierName = supplier.supplierName
        return vo
    }

    override fun update(supplierId: Long?, supplierName: String?, controlNo: String?) {
        supplierService!!.update(supplierId, supplierName, controlNo)
    }

    override fun delete(supplierId: Long?) {
        supplierService!!.delete(supplierId)
    }
}